import WeChatSDK from "../utils/WxSDK";
import { getMiniAppJSSDKTicket } from "../api/wx";
import { mapGetters } from 'vuex'

export default {
    data() {
        return {
            wxSDK: null,
            isReady: false
        }
    },
    computed: {
        ...mapGetters(['userInfo']),
    },
    watch: {
        userInfo: {
            immediate: true,
            deep: true,
            handler(userInfo) {
                if (userInfo && userInfo.userId) {
                    if ((this.$options.name || '').startsWith('wx_pages_')) {
                        this.initWxSDK()
                    }
                }
            },
        },
    },
    mounted() {
    },
    methods: {
        initWxSDK() {
            getMiniAppJSSDKTicket().then(res => {
                this.wxSDK = new WeChatSDK(res.data)
                this.wxSDK.init().then(() => {
                    this.isReady = true
                })
            })
        }
    }
};